<script setup>
import { useChannelStore } from "@/stores";
import { storeToRefs } from "pinia";
import { Edit, Delete } from "@element-plus/icons-vue";

const channelStore = useChannelStore();
const { channelList, dialogVisible, dialogForm, title, form } =
  storeToRefs(channelStore);
const {
  getChannels,
  handleEdit,
  handleDelete,
  addChannel,
  close,
  rules,
  submit,
} = channelStore;

getChannels();
</script>

<template>
  <CardPage>
    <template #title>文章分类</template>
    <template #button>
      <el-button type="primary" @click="addChannel()">添加分类</el-button>
    </template>
    <!-- 文章管理信息的表格 -->
    <el-table :data="channelList" style="width: 100%" size="large">
      <el-table-column type="index" label="序号" width="150" />
      <el-table-column prop="cate_name" label="分类名称" />
      <el-table-column prop="cate_alias" label="分类别名" />
      <el-table-column label="操作" width="200">
        <template #default="scope">
          <el-button
            type="primary"
            :icon="Edit"
            circle
            plain
            @click="handleEdit(scope.row)"
          ></el-button>
          <el-button
            type="danger"
            :icon="Delete"
            plain
            circle
            @click="handleDelete(scope.row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 编辑和添加分类的弹窗 -->
    <el-dialog v-model="dialogVisible" :title="title" width="600">
      <el-form :model="dialogForm" :rules="rules" ref="form">
        <el-form-item prop="cate_name">
          分类名称<el-input v-model="dialogForm.cate_name" />
        </el-form-item>
        <el-form-item prop="cate_alias">
          分类别名<el-input v-model="dialogForm.cate_alias" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="close()">取消</el-button>
          <el-button type="primary" @click="submit(dialogForm)">
            确认
          </el-button>
        </div>
      </template>
    </el-dialog>
  </CardPage>
</template>
<style scoped>
.el-dialog .el-form {
  display: flex;
  flex-direction: column;
  padding: 1vw;
}
.el-form .el-input {
  width: 80%;
  margin-left: 1vw;
}
</style>
